#include "DeviceParameterWidget.h"

#include "DeviceManager.h"

DeviceParameterWidget::DeviceParameterWidget(const QString& deviceName,
                                             const QVariantMap& configMap, QWidget* parent)
    : QGroupBox(deviceName, parent),
      m_deviceName(deviceName),
      m_configMap(configMap),
      m_gridLayout(nullptr),
      m_saveButton(nullptr)
{
    setupUI();
    connectSignals();
}

DeviceParameterWidget::~DeviceParameterWidget() {}

void DeviceParameterWidget::setupUI()
{
    m_gridLayout = new QGridLayout(this);

    int row = 0;
    // 根据配置映射动态创建输入框
    for (auto it = m_configMap.begin(); it != m_configMap.end(); ++it) {
        const QString& configKey = it.key();
        QVariant configValue = it.value();

        // 创建标签
        QLabel* label = new QLabel(configKey + ":", this);
        label->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
        label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        label->setMaximumHeight(35);
        label->setMaximumWidth(120);

        // 创建输入框
        QWidget* spinBox = createComponents(configValue);

        // 存储到映射中
        m_spinBoxMap[configKey] = spinBox;

        // 添加到布局
        int col = row % 2;
        int currentRow = row / 2;
        m_gridLayout->addWidget(label, currentRow, col * 2);
        m_gridLayout->addWidget(spinBox, currentRow, col * 2 + 1);

        row++;
    }

    // 添加保存按钮
    m_saveButton = new QPushButton("保存", this);
    int saveRow = (row + 1) / 2;
    m_gridLayout->addWidget(m_saveButton, saveRow, 3);
}

QAbstractSpinBox* DeviceParameterWidget::createComponents(QVariant value)
{
    QAbstractSpinBox* abstractSpinBox = nullptr;
    if (value.type() == QVariant::Int) {
        QSpinBox* spinBox = new QSpinBox(this);
        spinBox->setRange(0, 9999);
        spinBox->setValue(value.toInt());
        abstractSpinBox = spinBox;
    } else if (value.type() == QVariant::Double) {
        QDoubleSpinBox* spinBox = new QDoubleSpinBox(this);
        spinBox->setRange(0.0, 9999.99);
        spinBox->setDecimals(2);
        spinBox->setValue(value.toDouble());
        abstractSpinBox = spinBox;
    }
    if (abstractSpinBox) {
        abstractSpinBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        abstractSpinBox->setMaximumHeight(35);
        abstractSpinBox->setMaximumWidth(120);
        abstractSpinBox->setAlignment(Qt::AlignCenter);
    }
    return abstractSpinBox;
}
void DeviceParameterWidget::connectSignals()
{
    if (m_saveButton) {
        connect(m_saveButton, &QPushButton::clicked, this,
                &DeviceParameterWidget::onSaveButtonClicked);
    }
}

QVariantMap DeviceParameterWidget::getConfigValues() const
{
    QVariantMap configValues;
    for (auto it = m_spinBoxMap.begin(); it != m_spinBoxMap.end(); ++it) {
        QWidget* widget = it.value();
        if (qobject_cast<QSpinBox*>(widget)) {
            configValues[it.key()] = qobject_cast<QSpinBox*>(widget)->value();
        } else if (qobject_cast<QDoubleSpinBox*>(widget)) {
            configValues[it.key()] = qobject_cast<QDoubleSpinBox*>(widget)->value();
        }
    }
    return configValues;
}

void DeviceParameterWidget::updateConfigValues(const QVariantMap& configMap)
{
    m_configMap = configMap;

    // 更新现有输入框的值
    for (auto it = configMap.begin(); it != configMap.end(); ++it) {
        const QString& configKey = it.key();
        QVariant configValue = it.value();
        if (configValue.type() == QVariant::Int) {
            QSpinBox* spinBox = qobject_cast<QSpinBox*>(m_spinBoxMap[configKey]);
            if (spinBox) {
                spinBox->setValue(configValue.toInt());
            }
        } else if (configValue.type() == QVariant::Double) {
            QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox*>(m_spinBoxMap[configKey]);
            if (spinBox) {
                spinBox->setValue(configValue.toDouble());
            }
        }
    }
}

void DeviceParameterWidget::onSaveButtonClicked()
{
    emit parametersChanged(m_deviceName, getConfigValues());
}

void DeviceParameterWidget::showEvent(QShowEvent* event)
{
    QGroupBox::showEvent(event);
    DeviceManager* deviceManager = DeviceManager::instance();
    IDevice* device = deviceManager->getDeviceHelper(m_deviceName);
    bool isEnabled = device && device->isEnabled();
    this->setEnabled(isEnabled);
}